package com.cw.jeeyt.dao.impl.git;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.cw.jeeyt.common.utils.StringUtil;
import com.cw.jeeyt.dao.intf.git.GitBranchDao;
import com.cw.jeeyt.dao.mapper.git.GitBranchMapper;
import com.cw.jeeyt.dao.model.git.GitBranchDo;
import com.cw.jeeyt.dao.model.git.GitBranchExample;
import com.cw.jeeyt.dao.model.git.GitBranchKeyDo;
import com.cw.jeeyt.dao.query.git.GitBranchQuery;
import com.cw.lang.common.dao.AbstractBaseDaoImpl;
import com.cw.lang.common.pagehelper.PageHelper;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * git项目分支信息数据库操作接口实现
 *
 * @author chenw
 * @date 2019-11-24 16:15:01
 */
@Repository
public class GitBranchDaoImpl extends AbstractBaseDaoImpl<GitBranchDo, GitBranchMapper> implements GitBranchDao {
    /**
     * 分页查询
     *
     * @param gitBranch 条件
     * @param pageParam 分页参数
     * @return
     */
    @Override
    public PageInfo<GitBranchDo> selectPage(GitBranchQuery gitBranch, PageParam pageParam) {
        GitBranchExample example = buildExample(gitBranch);
        return PageHelper.<GitBranchDo>startPage(pageParam.getPageNum(), pageParam.getPageSize()).doSelectPageInfo(
            () -> getMapper().selectByExample(example));
    }

    /**
     * 根据key查询git项目分支信息
     *
     * @param id
     * @return
     */
    @Override
    public GitBranchDo selectByKey(Long id) {
        GitBranchKeyDo gitBranchKeyDo = new GitBranchKeyDo();
        gitBranchKeyDo.setId(id);
        return getMapper().selectByPrimaryKey(gitBranchKeyDo);
    }

    /**
     * 根据key删除git项目分支信息
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByKey(Long id) {
        GitBranchKeyDo gitBranchKeyDo = new GitBranchKeyDo();
        gitBranchKeyDo.setId(id);
        return getMapper().deleteByPrimaryKey(gitBranchKeyDo);
    }

    /**
     * 查询git项目分支信息
     *
     * @param gitBranch 条件
     * @return List<GitBranchDo>
     */
    @Override
    public List<GitBranchDo> selectByExample(GitBranchQuery gitBranch) {
        return getMapper().selectByExample(buildExample(gitBranch));
    }

    /**
     * 新增git项目分支信息
     *
     * @param gitBranch 条件
     * @return int>
     */
    @Override
    public int insertBySelective(GitBranchDo gitBranch) {
        if (gitBranch == null) {
            return -1;
        }
        return getMapper().insertSelective(gitBranch);
    }

    /**
     * 修改git项目分支信息
     *
     * @param gitBranch
     * @return
     */
    @Override
    public int updateBySelective(GitBranchDo gitBranch) {
        if (gitBranch == null) {
            return -1;
        }
        return getMapper().updateByPrimaryKeySelective(gitBranch);
    }

    /**
     * 构建git项目分支信息Example信息
     *
     * @param gitBranch
     * @return
     */
    public GitBranchExample buildExample(GitBranchQuery gitBranch) {
        GitBranchExample example = new GitBranchExample();
        GitBranchExample.Criteria criteria = example.createCriteria();
        if (gitBranch != null) {
            //添加查询条件
            if (null != gitBranch.getId()) {
                criteria.andIdEqualTo(gitBranch.getId());
            }
            if (null != gitBranch.getProjectId()) {
                criteria.andProjectIdEqualTo(gitBranch.getProjectId());
            }
            if (StringUtil.isNotEmpty(gitBranch.getBranchCode())) {
                criteria.andBranchCodeEqualTo(gitBranch.getBranchCode());
            }
            if (StringUtil.isNotEmpty(gitBranch.getBranchName())) {
                criteria.andBranchNameEqualTo(gitBranch.getBranchName());
            }
        }
        return example;
    }
}
